/*
* HomePageServiceImpl.java
* Created on  2014-5-14 下午8:44
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-5-14       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.homepage.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.homepage.dao.HomePageDao;
import com.genghis.ptas.homepage.service.HomePageService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Service("homePageService")
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private HomePageDao homePageDao;

    @Autowired
    private UserService userService;

    @Override
    public Page<CourseInfo> findCarouselPageList(PageParameter pageParameter, CourseInfo courseInfo) {
        return homePageDao.findCarouselPageList(pageParameter, courseInfo);
    }

    @Override
    public void editCarouselById(CourseInfo courseInfo) {
        homePageDao.editCarouselById(courseInfo);
    }

    @Override
    public List<CourseInfo> findCourseHotList() {
        String userName = LoginUserUtil.findLoginUserName();
        int userId = null == userName ? DictConstant.MINUS_ONE : userService.findFullUserByUserLoginName(userName).getId();
        List<CourseInfo> hotCourse = homePageDao.findCourseHotList(userId);
        for (CourseInfo course : hotCourse) {
            course.setIsRegister(null != course.getShowRegister() && !"".equals(course.getShowRegister()));
        }
        return hotCourse;
    }

    @Override
    public List<CourseInfo> findCourseSystemList() {
        String userName = LoginUserUtil.findLoginUserName();
        int userId = null == userName ? DictConstant.MINUS_ONE : userService.findFullUserByUserLoginName(userName).getId();
        List<CourseInfo> systemCourse = homePageDao.findCourseSystemList(userId);
        for (CourseInfo course : systemCourse) {
            course.setIsRegister(null != course.getShowRegister() && !"".equals(course.getShowRegister()));
        }
        return systemCourse;
    }

    @Override
    public List<CourseInfo> findCourseInterestedListByCourseType(Integer courseType) {
        String userName = LoginUserUtil.findLoginUserName();
        int userId = null == userName ? DictConstant.MINUS_ONE : userService.findFullUserByUserLoginName(userName).getId();
        List<CourseInfo> interestedList = null != userName ? homePageDao.findCourseInterestedListByCourseType(userId) : homePageDao.findCourseSystemList(DictConstant.MINUS_ONE);
        interestedList = interestedList.size() < 5 ? homePageDao.findCourseSystemList(DictConstant.MINUS_ONE) : interestedList;
        for (CourseInfo course : interestedList) {
            course.setIsRegister(null != course.getShowRegister() && !"".equals(course.getShowRegister()));
        }
        return interestedList;
    }

    @Override
    public List<CourseInfo> findTopThreeCourseByType(String courseType) {
        return homePageDao.findTopThreeCourseByType(courseType);
    }

    @Override
    public List<CourseInfo> findTopCourseByTypeValueId(int typeValueId) {
        return homePageDao.findTopCourseByTypeValueId(typeValueId);
    }

    @Override
    public List<CourseInfo> queryAllStudy() {
        return homePageDao.queryAllStudy();
    }

    @Override
    public List<CourseInfo> queryRank(String page) {
        if ("1".equals(page)) {
            return homePageDao.queryRank(DateUtil.getAgoDate(Calendar.WEEK_OF_YEAR, -1));
        } else {
            return homePageDao.queryRank(DateUtil.getAgoDate(Calendar.MONTH, -1));
        }
    }
}
